<template>
  <div class="login_container" :style="loginbj">
    <div class="login_box">
      <div class="avatar_box">
        <img src="@/assets/img/tx.jpeg" alt="" />
      </div>
      <el-form
        label-width="0px"
        :model="form"
        :rules="rules"
        ref="loginformRef"
      >
        <!-- 用户名 -->
        <el-form-item class="inpit_1" prop="username">
          <el-input
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            clearable
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item class="inpit_1" prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 登录重置 -->
        <el-form-item class="anniu_1">
          <el-button type="primary" @click="formdl">登录</el-button>
          <el-button type="info" @click="formcz">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginbj: {
        backgroundImage: "url(" + require("@/assets/img/loginbj.jpeg") + ")",
        backgroundRepeat: "no-repeat",
        backgroundSize: "100% 100%",
      },
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "用户名至少3位至多10位",
            trigger: "blur",
          },
          {
            validator: (rule, value, callback) => {
              if (/^[^_][a-zA-Z0-9_\u4e00-\u9fa5]*[^_]$/.test(value) == false) {
                callback(new Error("用户名不能有空格开头结尾不能有下划线"));
              } else {
                callback();
              }
            },
            trigger: "blur", //这里还有blur触发方法； 如果设为none，那么只有this.$refs[formName]['validate']时才会验证
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "密码至少输入6到15位", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (/^\w+$/.test(value) == false) {
                callback(new Error("密码不能有空格开头结尾不能有下划线"));
              } else {
                callback();
              }
            },
            trigger: "blur", //这里还有blur触发方法； 如果设为none，那么只有this.$refs[formName]['validate']时才会验证
          },
        ],
      },
     
    };
  },
  methods: {
    formcz() {
      this.$refs.loginformRef.resetFields();
    },
    formdl() {
      this.$refs.loginformRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.form);
        // console.log(res);
        if (res.meta.status !== 200) return this.$message.error("登录失败");
        this.$message.success("登录成功");
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style scoped lang="less">
.login_container {
  height: 100%;
  width: 100%;
}
.login_box {
  width: 800px;
  height: 500px;
  border-radius: 5px;
  background-color: rgba(28, 18, 62, 0.3);
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    width: 190px;
    height: 190px;
    border: 1px solid rgb(62, 155, 201);
    border-radius: 50%;
    box-shadow: 0 0 10px rgb(62, 155, 201);
    position: relative;
    top: 0px;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      width: 190px;
      height: 190px;
      border-radius: 50%;
    }
  }
}
.inpit_1 {
  width: 400px;
  position: relative;
  top: 0px;
  left: 50%;
  transform: translate(-50%, -50%);
}
.anniu_1 {
  position: absolute;
  top: 70%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>